#!/bin/bash

# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

# Initialization
shopt -s extglob
LOGFILE=/dev/null
if ((0)); then
	LOGFILE=$(date +"%y%m%d_%H%M%S").log
	touch $LOGFILE
	chown $(stat -c %U $0) $LOGFILE
fi
ACTION=add
GROUP="students";
NUMOFACCS=1;
ACCPREF="stud"
STARTIDX=01
SKEL=$(pwd)/$(dirname $0)/skel
#PASSWORD=$(mkpasswd abc123)
PASSWORD=oi/iO2Qg27Xyc

# Print usage info
function usage {
	echo "Usage: ..."
}

# Parse command line options
for opt in $@
do
	case $opt in
		--start=*)
			STARTIDX=$(echo $opt | sed 's/[-a-zA-Z0-9]*=//')
			;;
		--prefix=*)
			ACCPREF=$(echo $opt | sed 's/[-a-zA-Z0-9]*=//')
			;;
		--action=*)
			ACTION=$(echo $opt | sed 's/[-a-zA-Z0-9]*=//')
			;;
    	--num=*)
			NUMOFACCS=$(echo $opt | sed 's/[-a-zA-Z0-9]*=//')
			;;
    	--group=*)
			GROUP=$(echo $opt | sed 's/[-a-zA-Z0-9]*=//')
			;;
    	*)
			echo "Unrecognized command-line option '$opt'." 1>&2
			echo $(usage)
			exit 2
			;;
  	esac
done

# Use options to setup parameters
HOMEDIR=/home/$GROUP
if [[ $STARTIDX != ?(+)+([0-9]) ]]; then
	echo "Start index (--start=...) must be a non-negative number" 1>&2
	exit 3
fi
if [[ $NUMOFACCS != ?(+)+([0-9]) ]]; then
   echo "Number of accounts to be created (--num=...) must be non-negative" 1>&2
   exit 4
fi


# Action...
if [ $ACTION == add ]
then
	# Add students
	#--------------
	# Evaluate GID of the given group
	GID=$(cat /etc/group | grep "$GROUP:" |cut -d: -f3)
	# Check if HOME directory exists as a file
	if [ -f $HOMEDIR ]; then
   	echo "'$HOMEDIR' must not exist or be a directory" 1>&2
   	exit 5
	fi
	# Create HOME directory
	if [ ! -d $HOMEDIR ]
	then
		mkdir -p $HOMEDIR >> $LOGFILE
		[ $? -eq 0 ] && echo "Base directory '$HOMEDIR' has been created."
	fi
	# Create the group
	if [ -z $GID ]
	then
		# If the group does not exist we create it
		groupadd $GROUP >> $LOGFILE
		[ $? -eq 0 ] && GID=$(cat /etc/group | grep "$GROUP:" |cut -d: -f3)
		[ $? -eq 0 ] && echo "Group '$GROUP' has been created."
	fi
	# Create accounts
	for ((i=$STARTIDX;i<$NUMOFACCS+$STARTIDX;i++)); do
		username=$(printf "%s%02d" $ACCPREF $i)
		if [ -n "$(grep "$username:" /etc/passwd)" ]; then
			userdel --remove $username >> $LOGFILE
		fi
		printf "Creating account %-30s" "'$username'"
		STATUS="FAILED"
		useradd --gid $GID --groups "users" --base-dir $HOMEDIR --skel $SKEL --create-home --password $PASSWORD --shell /bin/bash $username >> $LOGFILE
		[ $? -eq 0 ] && STATUS="OK"
		echo $STATUS
		# Tweak password-less access to the account
		AUTHKEYS=$HOMEDIR/$username/.ssh/authorized_keys
		chown root:root $AUTHKEYS
		chmod 744 $AUTHKEYS
		chattr +i $AUTHKEYS
		passwd -e $username >> $LOGFILE
	done
else
	# Remove students
	#-----------------
	# Evaluate GID of the given group
	GID=$(cat /etc/group | grep "$GROUP:" |cut -d: -f3)
	if [ $GID ]
	then
		# Remove all members from the group
		for username in $(grep ":$GID:" /etc/passwd | cut -d: -f1)
		do
			printf "Removing account %-30s" "'$username'"
			chattr -i $HOMEDIR/$username/.ssh/authorized_keys
			STATUS="FAILED"
			userdel --remove $username >> $LOGFILE
			[ $? -eq 0 ] && STATUS="OK"
			echo $STATUS
		done
		# Remove the group
		groupdel $GROUP >> $LOGFILE
		[ $? -eq 0 ] && echo "Group '$GROUP' has been removed."
		# Remove the base directory
		rm -rf $HOMEDIR >> $LOGFILE
		[ $? -eq 0 ] && echo "Base directory '$HOMEDIR' has been removed."
	else
		echo "There is no group '$GROUP' -- nothing to remove."
	fi
fi
